/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.location.LocationService;
import com.aroundj2me.model.ContinentName;
import com.aroundj2me.utility.ImageLoader;

public class ContinentListView extends List implements CommandListener, IView{
	private ApplicationController controller;

	private Command installCommand;
	
	public ContinentListView(){
		super("Available Continents", List.IMPLICIT);
		
		createCommands();
		
		this.setCommandListener(this);
	}
	
	public ContinentListView(ApplicationController controller) {	
		this();
		this.controller = controller;
	}

	public void loadList() {
		this.deleteAll();
		this.append(ContinentName.EUROPE, loadImage(ContinentName.EUROPE));
		this.append(ContinentName.ASIA, loadImage(ContinentName.ASIA));
		this.append(ContinentName.AFRICA, loadImage(ContinentName.AFRICA));
		this.append(ContinentName.NORTHAMERICA, loadImage(ContinentName.NORTHAMERICA));
		this.append(ContinentName.SOUTHAMERICA, loadImage(ContinentName.SOUTHAMERICA));
		this.append(ContinentName.AUSTRALIA, loadImage(ContinentName.AUSTRALIA));
	}	
	
	private Image loadImage(String continent) {
		LocationService locationService = controller.getLocationService();
		if (locationService.existsLandmarkStore(continent)) {
			return ImageLoader.getImage(ImageLoader.DB_DOWNLOADED);
		}
		return ImageLoader.getImage(ImageLoader.DB_DOWNLOAD);
		
	}
	
	private void createCommands(){
		installCommand = new Command("Install", "Install continent", Command.SCREEN,0);
		this.addCommand(installCommand);
	}

	public void commandAction(Command c, Displayable d) {
		if(c.getCommandType() == Command.BACK)
			ScreenController.getInstance().last();
		else if(d == this || c == installCommand){
			controller.executeContinentRequest();
		}
	}
	
	public String getSelectedContinent(){
		return this.getString(this.getSelectedIndex());
	}
	
	public void setController(ApplicationController controller) {
		this.controller = controller;
	}
}
